var Md5 = require('../../utils/md5.js');
// var timeStamp = Date.now();
var timeSecond = Math.floor(Date.now() * 0.001);  //获取时间戳: 
var SignInfoData = Md5.hexMD5(timeSecond + 'nativetalk')//md5加密;
const app = getApp();
var httpNativetalk = app.globalData.httpNativetalk;
var httpWeike = app.globalData.httpWeike;
var goodsId, classId, set_id, money, weikeid, goodsCategoryCode, js_code, weikeVipPrice, payType, payWay = '余额支付';
Page({
  data: {
    payWay: '余额支付',
    imgUrl: 'http://file.waijiaojun.com/waijiaojun/icon',
  },
  onLoad: function (e) {
    console.log(e);
    weikeVipPrice = e.weikeVipPrice;
    weikeid = e.weikeid;
    set_id = e.set_id;
    money = e.set_amout;
    classId = e.classId;
    var that = this;
    //获取余额
    wx.request({
      url: httpNativetalk + '/app/3/token/member/balance',
      data: {

      },
      header: {
        "Timestamp": timeSecond,
        "SignInfo": SignInfoData,
        "content-Type": "application/json",
        "client_type": "3",
        "version": 15,
        'token': app.globalData.token,
      },
      method: 'POST',
      success: function (res) {
       
        that.setData({
          memb_balance: res.data.obj.memb_balance,
          payMoney: e.set_amout,
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  //支付方式的选择
  payWayFun: function (e) {
    payWay = e.currentTarget.dataset.text;
    this.setData({
      payWay: payWay,
    });
   
  },
  //确认支付
  sureBtnFun: function (e) {
    // console.log(payWay)
    if (weikeid !== undefined) {
      
      goodsCategoryCode = "sys_course";
      goodsId = weikeid;
    };
    if (set_id !== undefined) {
      
      goodsCategoryCode = "course";
      goodsId = set_id;
    };
    if (weikeVipPrice !== undefined) {
     
      goodsCategoryCode = "sys_course_vip_year";
      goodsId = '';
    };
    if (payWay == '余额支付') {
      console.log(payWay, goodsCategoryCode, goodsId)
      wx.request({
        url: httpWeike + '/payment/token/create/order',
        data: {
          "goodsCategoryCode": goodsCategoryCode,
          "goodsId": goodsId,
          "payType": "wallet",
          "money": money,
        },
        header: {
          "Timestamp": timeSecond,
          "SignInfo": SignInfoData,
          "content-Type": "application/json",
          "client_type": "3",
          "version": 15,
          'token': app.globalData.token,
        },
        method: 'POST',
        success: function (res) {
          console.log(res);
          if (res.data.mark == 1) {
            wx.showToast({
              title: res.data.tip,
              icon: 'loading',
              duration: 2000
            })
          }
          if (res.data.tip == '订单创建成功') {
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              duration: 2000,
              success: function (res) {
                setTimeout(function () {                 
                  if (goodsCategoryCode == 'course') {
                    wx.navigateBack({
                      delta: 1
                    })
                  }
                  if (goodsCategoryCode == 'sys_course') {
                    wx.navigateBack({
                      delta: 1
                    })
                  }
                }, 2000)
              }
            })
          }
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    };
    if (payWay == '微信支付') {
      //console.log('微信支付')
      wx.login({
        success: res => {
          js_code = res.code;
          
          wx.request({
            url: httpWeike + "/wechatSmallProgram/token/payment",
            data: {
              "goodsCategoryCode": goodsCategoryCode,
              "goodsId": goodsId,
              "payType": "thirdpart",
              "channel": "wx",
              "money": money,
              "code": js_code,
            },
            header: {
              "Timestamp": timeSecond,
              "SignInfo": SignInfoData,
              "content-Type": "application/json",
              "client_type": "3",
              "version": 15,
              'token': app.globalData.token,
            },
            method: 'POST',
            success: function (res) {
              //console.log(res);
              var resData = res.data.obj;
              wx.requestPayment({
                'appId': resData.appId,
                'timeStamp': resData.timeStamp,
                'nonceStr': resData.nonceStr,
                'package': resData.data,
                'signType': resData.signType,
                'paySign': resData.paySign,
                'success': function (res) {
                  //console.log(res)
                },
                'fail': function (res) {
                }
              })
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        }
      })
    }
  }
})